C# Read Byte [] to Image

Posted by LucasGuitar on Stack Overflow See other posts from Stack Overflow or by LucasGuitar
Published on 2014-06-13T02:46:44Z Indexed on 2014/06/13 3:25 UTC
Read the original article Hit count: 157

Filed under:
|
|
|
|

I have an application which I'm adding pictures and these are automatically converted to binary and stored in a single file. how can I save several images, I keep in an XML file start and size of each set of refente to an image byte. But it has several images in bytes, whenever I try to select a different set of bytes just opening the same image. I would like your help to be able to fix this and open different images.

Code

//Add Image

private void btAddImage_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Title = "Selecione a Imagem";
            op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
                "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
                "Portable Network Graphic (*.png)|*.png";

            if (op.ShowDialog() == true)
            {
                imgPatch.Source = new BitmapImage(new Uri(op.FileName));
                txtName.Focus();
            }
        }


//Convert Image

 private void btConvertImage_Click(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(txtName.Text))
            {
                txtName.Focus();
                MessageBox.Show("Preencha o Nome", "Error");
            }

            else
            {
                save(ConvertFileToByteArray(op.FileName), txtName.Text);
            }
        }

//Image to Byte Array

private static byte[] ConvertFileToByteArray(String FilePath)
        {
            return File.ReadAllBytes(FilePath);
        }


//Save Binary File and XML File

public void save(byte[] img, string nome)
        {
            FileStream f;
            long ini, fin = img.Length; 

            if (!File.Exists("Escudos.bcf"))
            {
                f = new FileStream("Escudos.bcf", FileMode.Create);
                ini = 0;
            }

            else
            {
                f = new FileStream("Escudos.bcf", FileMode.Append);
                ini = f.Length + 1;
                bin = new TestBinarySegment();

            }

            bin.LoadAddSave("Escudos.xml", "Brasileiro", nome, ini, fin);



            BinaryWriter b = new BinaryWriter(f);
            b.Write(img);
            b.Close();
            f.Dispose();



        }


//Load Image from Byte
private void btLoad_Click(object sender, RoutedEventArgs e)
        {
            getImageFromByte();
        }


//Byte to Image
public void getImageFromByte(int start, int length)
        {
            using (FileStream fs = new FileStream("Escudos.bcf", FileMode.Open))
            {
                byte[] iba = new byte[fs.Length+1];
                fs.Read(iba, start, length);
                Image image = new Image();
                image.Source = BitmapFrame.Create(fs, BitmapCreateOptions.None,
                                          BitmapCacheOption.OnLoad);

                imgPatch2.Source = image.Source;
            }
        }

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf